package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.cucc.service.attr.handler.AccessoriesOthersAttrHandler;
import com.iwhalecloud.bss.kite.cucc.service.pos.handler.CuccPosServiceUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@AttrHandler(AccessoriesOthersAttrHandler.class)
public class AccessoriesOthersAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        KiteOfferInst giftOfferInst = querySceneInstService.getGiftOfferInst(data.getSceneInstId());
        String cateId = CuccPosServiceUtils.getCateId(data.getSceneInstId(), data.getCustId());
        String offerId = giftOfferInst.getSpecId();
        if("2004109".equals(cateId) && "30000281".equals(offerId)){
            linkDealPrice(attr,inst,data);
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
/**
 * 联动处理公允价、物资编码
 */
    private void linkDealPrice(KiteAttr attr, KiteInst inst, AttrData data){
        //公允价
        KiteAttr fairPriceAttr = inst.getAttr("200004115");
        //物资编码
        KiteAttr materialCodeAttr = inst.getAttr("200004178");

        if(Objects.nonNull(fairPriceAttr)){
            String fairPrice = AttrValueCache.getAttrValueByAttrValueName(fairPriceAttr.getAttrId(), attr.getValueDesc());
            operateInstAttrService.setAttrValue(inst,"200004115", fairPrice, data.getSceneInstId(),null);
        }
        if(Objects.nonNull(materialCodeAttr)){
            String fairPrice = AttrValueCache.getAttrValueByAttrValueName(materialCodeAttr.getAttrId(), attr.getValueDesc());
            operateInstAttrService.setAttrValue(inst,"200004178", fairPrice, data.getSceneInstId(),null);
        }
    }
}
